package com.jerei.store.controller;

import com.jerei.store.entity.TableCart;
import com.jerei.store.entity.TableCartProduct;
import com.jerei.store.service.ITableCartService;
import com.jerei.store.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/tableCart")
public class TableCartController extends BaseController{
    @Autowired
    private ITableCartService service;

    @RequestMapping("/add")
    public JsonResult<Void> addTableCart(Integer pid, Integer num,HttpSession session){
        System.out.println("添加到购物车的数量 num="+num);
        Integer uid=getUidFromSession(session);
        String username=getUsernameFromSession(session);
        service.addTC(uid,username,pid,num);
        return new JsonResult<>(OK);
    }


    @RequestMapping("/findAll")
    public JsonResult<List<TableCartProduct>> findAllByUid(HttpSession session){
        Integer uid=getUidFromSession(session);

        List<TableCartProduct> list =service.findAllByUid(uid);
        return new JsonResult<>(OK,list);
    }

    @RequestMapping("/del")
    public JsonResult<Void> delAllByCid(Integer[] arr){
        System.out.println("数组里面的元素"+Arrays.toString(arr));
        service.delAllByCid(arr);
        return new JsonResult<>(OK);
    }


    @RequestMapping("/changeNum")
    public JsonResult<Void> addNumByCid(Integer cid,Integer val,HttpSession session){
        String username=getUsernameFromSession(session);
        service.changeNum(val,cid,username);
        return new JsonResult<>(OK);
    }

    @RequestMapping("/findAllByCid")
    public JsonResult<List<TableCartProduct>> findAllByCid(Integer[] arr){

        System.out.println("arr="+Arrays.toString(arr));
        List<TableCartProduct> list= service.findAllByCid(arr);
        return new JsonResult<>(OK,list);
    }


}
